package com.sihuatech.coreagent.controller;

import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sihuatech.coreagent.service.ScspSendService;
import com.sihuatech.coreagent.until.XmlParser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api
@Controller
public class ScspController {
	private static final Logger logger = LoggerFactory.getLogger(ScspController.class);
	@Autowired
	private HttpServletRequest req;
	@Autowired
	private ScspSendService scspSendService;
	
	@ApiOperation("分省发往中心路由请求入口")
	@ApiImplicitParam(name = "xml", value = "接收的请求报文")
	@PostMapping("/scspToAgent")
	@ResponseBody
	public String toAgentBusiness() {
		
		String request = XmlParser.getXml(req);
		if (StringUtils.isBlank(request))
			logger.error("请求报文为空");
		else
			logger.info("请求报文为：\n" + request);

		try {
			// 创建SAXReader
			SAXReader saxReader = new SAXReader();
			//从ByteArrayInputStream字节数组输入流获取数据
			Document document = saxReader.read(new ByteArrayInputStream(request.getBytes()));
			// 获取根节点 
			Element root = document.getRootElement();
			String command = root.getName();
			logger.info("command:"+command);
			String orgTransactionId="";//原业务流水字段前两位为省份字段缩写
			if (StringUtils.isNotBlank(command)) {
				if("advPay".equals(command)){
					logger.info("二维码创建、支付查询、退款接口");
					String bufiCode = StringUtils.substringBetween(request, "<busiCode>","</busiCode>");
					orgTransactionId = StringUtils.substringBetween(request, "<orgTransactionId>","</orgTransactionId>");
                    if(StringUtils.isNotBlank(bufiCode) && ("3029".equals(bufiCode)||"3088".equals(bufiCode))){
                    	orgTransactionId = StringUtils.substringBetween(request, "<transactionId>","</transactionId>");	
					}
					String proviceId = StringUtils.substring(orgTransactionId, 0, 2);
					String advPayResp = scspSendService.sendToThird(request, bufiCode,proviceId,"1");
					logger.info("一级支付中心回执报文:" + advPayResp);
					return advPayResp;
				}
			}
		} catch (DocumentException e) {
			logger.error("获取报文根节点失败", e);
		}
		return null;
	}
	
	@ApiOperation("分省发往中心路由请求入口")
	@ApiImplicitParam(name = "xml", value = "接收的请求报文")
	@PostMapping("/vasToAgent")
	@ResponseBody
	public String vasToAgentBusiness() {
		
		String request = XmlParser.getXml(req);
		if (StringUtils.isBlank(request))
			logger.error("请求报文为空");
		else
			logger.info("请求报文为：\n" + request);

		try {
			// 创建SAXReader
			SAXReader saxReader = new SAXReader();
			//从ByteArrayInputStream字节数组输入流获取数据
			Document document = saxReader.read(new ByteArrayInputStream(request.getBytes()));
			// 获取根节点 
			Element root = document.getRootElement();
			String command = root.getName();
			logger.info("command:"+command);
			String orgTransactionId="";//原业务流水字段前两位为省份字段缩写
			if (StringUtils.isNotBlank(command)) {
				if("advPay".equals(command)){
					logger.info("二维码创建、支付查询、退款接口");
					String bufiCode = StringUtils.substringBetween(request, "<busiCode>","</busiCode>");
					orgTransactionId = StringUtils.substringBetween(request, "<orgTransactionId>","</orgTransactionId>");
                    if(StringUtils.isNotBlank(bufiCode) && ("3005".equals(bufiCode)||"3089".equals(bufiCode))){
                    	orgTransactionId = StringUtils.substringBetween(request, "<transactionId>","</transactionId>");	
					}
					String proviceId = StringUtils.substring(orgTransactionId, 0, 2);
					String advPayResp = scspSendService.sendToThird(request, bufiCode,proviceId,"1");
					logger.info("一级支付中心回执报文:" + advPayResp);
					return advPayResp;
				}
			}
		} catch (DocumentException e) {
			logger.error("获取报文根节点失败", e);
		}
		return null;
	}
	
	
	@ApiOperation("中心路由接收第三方请求入口")
	@ApiImplicitParam(name = "xml", value = "接收的请求报文")
	@PostMapping("/advPayToAgent")
	@ResponseBody
	public String chargingCenterBusiness() {
		
		String request = XmlParser.getXml(req);
		if (StringUtils.isBlank(request))
			logger.error("请求报文为空");
		else
			logger.info("请求报文为：\n" + request);

		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new ByteArrayInputStream(request.getBytes()));
			Element root = document.getRootElement();
			String command = root.getName();
			logger.info("command:"+command);
			String orgTransactionId="";//原业务流水字段前两位为省份字段缩写
			if (StringUtils.isNotBlank(command)) {
				if ("advPay".equals(command)) {
					logger.info("支付通知、退款通知接口");
					// 截取orgTransactionId获取省份缩写 转发分省
					orgTransactionId = StringUtils.substringBetween(request, "<orgTransactionId>","</orgTransactionId>");
					String proviceId = StringUtils.substring(orgTransactionId, 0, 2);
					String bufiCode = StringUtils.substringBetween(request, "<busiCode>","</busiCode>");
					String resp = scspSendService.sendToProvince(request,proviceId,bufiCode,"6");
					logger.info("分省响应:" + resp);
					return resp;
				} 
			}
		} catch (DocumentException e) {
			logger.error("获取报文根节点失败", e);
		}
		return null;
	}
	
	@ApiOperation("中心路由接收第三方请求入口")
	@ApiImplicitParam(name = "xml", value = "接收的请求报文")
	@PostMapping("/vasAdvPayToAgent")
	@ResponseBody
	public String vasChargingCenterBusiness() {
		
		String request = XmlParser.getXml(req);
		if (StringUtils.isBlank(request))
			logger.error("请求报文为空");
		else
			logger.info("请求报文为：\n" + request);

		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new ByteArrayInputStream(request.getBytes()));
			Element root = document.getRootElement();
			String command = root.getName();
			logger.info("command:"+command);
			String orgTransactionId="";//原业务流水字段前两位为省份字段缩写
			if (StringUtils.isNotBlank(command)) {
				if ("advPay".equals(command)) {
					logger.info("支付通知、退款通知接口");
					// 截取orgTransactionId获取省份缩写 转发分省
					orgTransactionId = StringUtils.substringBetween(request, "<orgTransactionId>","</orgTransactionId>");
					String proviceId = StringUtils.substring(orgTransactionId, 0, 2);
					String bufiCode = StringUtils.substringBetween(request, "<busiCode>","</busiCode>");
					String resp = scspSendService.sendToProvince(request,proviceId,bufiCode,"5");
					logger.info("分省响应:" + resp);
					return resp;
				} 
			}
		} catch (DocumentException e) {
			logger.error("获取报文根节点失败", e);
		}
		return null;
	}
	
}
